import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// Runnable 不能获取返回值
class MyRunnable implements Runnable {

    private final String name;
    private final int waitTime;

    MyRunnable(String name, int waitTime) {
        this.name = name;
        this.waitTime = waitTime;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " Start. Time = " + new Date());
        processCommand();
        System.out.println(Thread.currentThread().getName() + " End. Time = " + new Date());
    }

    private void processCommand() {
        try {
            Thread.sleep(waitTime);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return this.name;
    }
}

public class threadPool {

    private static final int CORE_POOl_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 100;
    private static final long KEEP_LIVE_TIME = 1L;


    public static void main(String[] args) {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                CORE_POOl_SIZE,
                MAX_POOL_SIZE,
                KEEP_LIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.AbortPolicy()
                );

        for (int i = 0; i < 10; i++) {
            Runnable task = new MyRunnable("" + i, i);
            threadPool.execute(task);

            /*
            threadPool.execute(
                    () -> {
                        System.out.println("方法块");
                    }
            );
            */
        }

        threadPool.shutdown();
        while (!threadPool.isTerminated()) {
            // System.out.println("No finish!");
        }

        System.out.println("Finished!");
    }
}
